package com.wsc.crawler.grabber;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.wsc.crawler.URLutils.RefineURL;
import com.wsc.crawler.init.CrawlerConfig;

/**
 * Example program to list links from a URL.
 */
public class HyperLinkParser {

	private CrawlerConfig config;
	private Document doc;
	private Elements links;
	private Elements iframeLinks;
	private Elements framesrclinks;
	private Logger log=Logger.getLogger(HyperLinkParser.class.getName());
	private String baseUrl ;
	
	public HyperLinkParser(String html, String baseUri)
			throws UnsupportedEncodingException {

		doc = Jsoup.parse(html, baseUri);
		links = doc.select("a[href]"); // modify this according to the config
		iframeLinks = doc.select("iframe[src]");// modify this according to the
		framesrclinks = doc.select("frame[src]");// modify this according to the config
		this.baseUrl = baseUri;
	}
	
	public List<String> getHyperLinks(){
		
		log.debug("Parsing HyperLinks in ("+this.baseUrl+").");
		
		List<String> hyperLinks=new ArrayList<String>();
		 
		for (Element link : links) {
			String s = link.attr("abs:href");
			if (!s.isEmpty() && s.startsWith("http")) {
				hyperLinks.add(s);
			}
		}
		log.debug("Parsed hyperLinks size in ("+this.baseUrl+") is "+hyperLinks.size());
		return hyperLinks;
	}
	
	public List<String> getIframeLinks(){
		log.debug("Parsing IframeLinks in ("+this.baseUrl+").");
		List<String> iframeLinksList = new ArrayList<String>();
		
		for (Element link : iframeLinks) {
			String frmsrc=link.attr("abs:src");
			if (!frmsrc.isEmpty() && frmsrc.startsWith("http")) {
		
				iframeLinksList.add(frmsrc);
			}
		}
		log.debug("Parsed IframeLinks size in ("+this.baseUrl+") is "+iframeLinksList.size());
		return iframeLinksList;
	}

	

	public List<String> getframeSrcLinks(){
		log.debug("Parsing frame Source Links in ("+this.baseUrl+").");
		List<String> frameSrcLinksList = new ArrayList<String>();
		
		for (Element link : framesrclinks) {
			
			String frmsrc=link.attr("abs:src");
			if (!frmsrc.isEmpty() && frmsrc.startsWith("http")) {
		
				frameSrcLinksList.add(frmsrc);
			}
		}
		log.debug("Parsed IframeLinks size in ("+this.baseUrl+") is "+frameSrcLinksList.size());
		return frameSrcLinksList;
	}

	
	public List<String> getAllLinksInPage(){
		
		log.debug("All hyperLinks are adding to one list from ("+this.baseUrl+").");
		
		List<String> allLinks=new ArrayList<String>();
		
		// each of the list size must not be empty 
		if(!getHyperLinks().isEmpty())
		{
			allLinks.addAll(getHyperLinks());
		}
		
		if(!getIframeLinks().isEmpty())
		{
			allLinks.addAll(getIframeLinks());
		}
		
		if(!getframeSrcLinks().isEmpty()) 
		{
			allLinks.addAll(getframeSrcLinks());
		}
		
		log.debug("Before duplicate Elimination size of hyperLinks found in ("+this.baseUrl+") is "+allLinks.size());
		
		// Remove fragment part of all urls
		allLinks = RefineURL.removeFragments(allLinks);
		
		// remove duplicates		
		allLinks = removeDuplicates(allLinks);
		
		log.debug("After duplicate Elimination size of hyperLinks found in ("+this.baseUrl+") is "+allLinks.size());

		return allLinks;
	}
	
	// remove duplicate urls from list
	
   private List<String> removeDuplicates(List<String> arlist){
           HashSet<String> hs=new HashSet<String>(arlist);
           ArrayList<String> tmp=new ArrayList<String>();
           tmp.addAll(hs);
           return tmp;
   }
	   
	
	/*
	 * Add Method to eliminate visited urls by requesting (VStatus) indexer.
	 */

	/*
	 * Method to call filterWSDL method from filter here.
	 * 
	 * return WSDL URL list to handler.
	 */

	
	public CrawlerConfig getConfig() {
		return config;
	}

	
	
	public void setConfig(CrawlerConfig config) {
		this.config = config;
	}
 
}
